﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lacrima.Gobang.Infrastructure
{
    public static class ReverseMethod
    {
        /// <summary>
        /// 2018年1月26日 上午，乐普医疗面试题目
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ReverseWords(string source,char separator)
        {
            return string.Join(separator.ToString(), source.Split(separator).Reverse());
        }

        /// <summary>
        /// 手写分割算法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ReverseWordsWithSeparator(this string source, char separator)
        {
            char[] array = source.ToCharArray();
            int len = array.Length;
            List<string> words = new List<string>();
            int index = 0;
            int flushIndex = 0;
            while (index < len)
            {
                if (array[index] == separator)
                {
                    string word = source.Substring(flushIndex, index - flushIndex).Trim(separator);
                    words.Add(word);
                    flushIndex = index;
                }
                else if(index == len - 1)
                {
                    string word = source.Substring(flushIndex, index - flushIndex + 1).Trim(separator);
                    words.Add(word);
                    flushIndex = index;
                }
                index++;
            }

            return string.Join(separator.ToString(), words.Reverse<string>());
        }

        /// <summary>
        /// 根网科技面试题目
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ReverseEachWords(string source, char separator)
        {
            return string.Join(separator.ToString(), source.Split(separator).Reverse()
                         .Select(o => string.Join(string.Empty,o.Reverse())));
        }

        /// <summary>
        /// 手写分割算法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ReverseEachWordsWithSeparator(string source, char separator)
        {
            char[] arr = source.ToArray();
            char[] array = source.ToCharArray();
            int len = array.Length;
            List<string> words = new List<string>();
            int index = 0;
            int flushIndex = 0;
            while (index < len)
            {
                if (array[index] == separator)
                {
                    string word = source.Substring(flushIndex, index - flushIndex).Trim(separator);
                    words.Add(string.Join(string.Empty, word.Reverse()));
                    flushIndex = index;
                }
                else if (index == len - 1)
                {
                    string word = source.Substring(flushIndex, index - flushIndex + 1).Trim(separator);
                    words.Add(string.Join(string.Empty, word.Reverse()));
                    flushIndex = index;
                }
                index++;
            }

            return string.Join(separator.ToString(), words);
        }
    }
}
